package com.hzcx.app.simplechat.api;


import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class APIRequest {

    private static final String TAG = APIRequest.class.getSimpleName();
    private static int CONNECTTIMEOUT = 60000;
    private static int READTIMEOUT = 60000;

//    private static String TEST_BASEURL = "http://liaotian.cqhzcx.com/index.php/";

    private static String FORMAL_BASEURL = "http://android.dlchat.cc/index.php/";
//
//    private static String FORMAL_BASEURL = "https://app2021a.dlchat.net/index.php/";

//    private static String FORMAL_BASEURL = "http://dl.tx.leo83.com/index.php/";

    private static RequestInterface requestInterface;


    /**
     * 实例化okhttp对象
     */
    private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder()
            .addInterceptor(new HttpLogUtil())
            .addInterceptor(new BaseUrlManagerInterceptor())
            .connectTimeout(CONNECTTIMEOUT, TimeUnit.MILLISECONDS)
            .readTimeout(READTIMEOUT, TimeUnit.MILLISECONDS);

    /**
     * 实例化retrofit对象
     */
    private static Retrofit.Builder builder = new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create())  //数据解析器
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//支持rxjava平台
            .client(httpClient.build())
            .baseUrl(getBASEURL());

    private static String getBASEURL() {
        return FORMAL_BASEURL;
    }

    public static RequestInterface getRequestInterface() {
        if (requestInterface == null) {
            synchronized (RequestInterface.class) {
                if (requestInterface == null) {
                    requestInterface = builder.build().create(RequestInterface.class);
                }
            }
        }
        return requestInterface;
    }

}
